/*
 *  This file is part of the SIRIUS Software for analyzing MS and MS/MS data
 *
 *  Copyright (C) 2013-2020 Kai Dührkop, Markus Fleischauer, Marcus Ludwig, Martin A. Hoffman, Fleming Kretschmer, Marvin Meusel and Sebastian Böcker,
 *  Chair of Bioinformatics, Friedrich-Schiller University.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License along with SIRIUS.  If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>
 */

package de.unijena.bioinf.ms.gui.utils.jCheckboxList;

import de.unijena.bioinf.ms.gui.utils.RelativeLayout;
import de.unijena.bioinf.ms.gui.utils.TextHeaderBoxPanel;

import javax.swing.*;
import java.awt.*;

public class JCheckboxListPanel<E> extends TextHeaderBoxPanel {
    public final JCheckBoxList<E> checkBoxList;
    private final JButton all = new JButton("all");
    private final JButton none = new JButton("none");
    public final JPanel buttons;

    public JCheckboxListPanel(JCheckBoxList<E> sourceList, String headline, String tooltip) {
        this(sourceList, headline);
        setToolTipText(tooltip);
    }

    public JCheckboxListPanel(JCheckBoxList<E> sourceList, String headline) {
        super(headline);
        checkBoxList = sourceList;
        checkBoxList.setVisibleRowCount(6);

        JScrollPane sp = new JScrollPane(checkBoxList);
        sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        add(sp, BorderLayout.CENTER);

        all.addActionListener(e -> checkBoxList.checkAll());
        none.addActionListener(e -> checkBoxList.uncheckAll());
        RelativeLayout l = new RelativeLayout(RelativeLayout.X_AXIS, 1);
        l.setAlignment(RelativeLayout.CENTER);
        buttons = new JPanel(new FlowLayout());
        buttons.add(all);
        buttons.add(none);
        add(buttons, BorderLayout.SOUTH);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        checkBoxList.setEnabled(enabled);
        buttons.setEnabled(enabled);
        for (Component c : buttons.getComponents())
            c.setEnabled(enabled);
    }
}
